using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Utility;
using Entites;
using BUS;
using DAL;
public partial class admin_ThemSP : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
        }
    }
      private void LoadDropDownList()
    {
        tbDanhMucCollection dmCollection = DanhMucBUS.GetAll();
        ddl_danhmuc.DataTextField = tbDanhMucColumns.TenDanhMuc.ToString();
        ddl_danhmuc.DataValueField = tbDanhMucColumns.MaDM.ToString();
        ddl_danhmuc.DataSource = dmCollection;
        ddl_danhmuc.DataBind();
        ddl_danhmuc.Items.Insert(0, new ListItem("All", "0"));

        tbDoanhNghiepCollection dnCollection = DoanhNghiepBUS.GetAll();
        ddl_MaDN.DataTextField = tbDoanhNghiepColumns.TenDN.ToString();
        ddl_MaDN.DataValueField = tbDoanhNghiepColumns.MaDN.ToString();
        ddl_MaDN.DataSource = dnCollection;
        ddl_MaDN.DataBind();
        ddl_MaDN.Items.Insert(0, new ListItem("All", "0"));
    }
    protected void btn_them_Click(object sender, EventArgs e)
    {
          try
        {
            SanPham  sanpham = new SanPham ();
            sanpham.tensp = txt_TenSP.Text;
            sanpham.danhmuc  = int.Parse(ddl_danhmuc .SelectedValue);
            sanpham.doanhnghiep  = int.Parse(ddl_MaDN .SelectedValue);
         
            sanpham.giagoc = int.Parse(txt_GiaGoc .Text );
              sanpham.giaban = int.Parse(txt_GiaBan.Text);
              sanpham.ngaybatdau = DateTime.Parse(txt_NgayBatDau.Text);
              sanpham.ngayhethan = DateTime.Parse(txt_NgayHetHan.Text);
              sanpham.soluongban = float.Parse(txt_SoLuuongBan.Text);
              sanpham.soluongtoida = float.Parse(txt_SoLuongToiDa.Text);

            sanpham.mota = txtmota.Text;
              sanpham.chietkhau = float.Parse(txt_ChietKhau.Text);
              sanpham.tinhtrang = int.Parse(txt_TinhTrang.Text);


            if (fu_hinh.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_hinh.FileName;
                fu_hinh.SaveAs(Server.MapPath("~/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);

                sanpham.hinh = filename;

            }
            else
            {
                sanpham.hinh     = "no_images.jpg";
            }
           SanPhamBUS .Insert(sanpham);


        }
        catch (Exception ex)
        {
        }
        Response.Redirect("~/admin/DanhSachSP.aspx");
    }

    }

